package com.ruoyi.web.controller;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.properties.CasProperties;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author ：jijinwu
 * @ClassName AuthApi
 * @date ：2025/1/18 - 17:12
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
@RestController
@RequestMapping("/auth")
public class AuthApi {

    @Autowired
    private CasProperties casProperties;

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 是否开启cas认证登录
     * @return
     */
    @GetMapping("/isCasEnabled")
    public boolean isCasEnabled() {
        String casEnabled = sysConfigService.selectConfigByKey("sys.cas.sso.enabled");
        return StringUtils.isNotEmpty(casEnabled)
                ? Convert.toBool(casEnabled)
                : casProperties.isCasEnable();
    }


    /**
     * CAS 认证登录地址
     * @return
     */
    @GetMapping("/oauth2")
    public String auth(){
        return this.casProperties.getServerLoginUrl() +
                "?service=" +
                this.casProperties.getClientLoginUrl()+"/login";
    }


    /**
     *CAS 认证注销地址
     * @param session
     * @return
     */
    @GetMapping({"/casLogout"})
    public String casLogout(HttpSession session) {
        session.invalidate();
        return this.casProperties.getServerLogoutUrl() +
                "?service=" +
                this.casProperties.getClientLoginUrl();
    }
}

